<?php

namespace app\api\controller\kuyou\third;

use app\common\controller\Api;
use think\Db;
use think\Loader;
class Test extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        $data = input();
        $controllername = Loader::parseName($this->request->controller());
        $actionname = strtolower($this->request->action());

//        $goods = [
//            ['goods_code'=>'SP001','goods_unit'=>'3','goods_attr'=>'5','number'=>1,'price'=>20],
//            ['goods_code'=>'SP002','goods_unit'=>'2','goods_attr'=>'1','number'=>1,'price'=>100]
//        ];
//        $goods = json_encode($goods);
//        var_dump($goods);die;

        //验证账户是否可以以及对应秘钥
        $account = isset($data['account'])?$data['account']:'';
        $requestSign = isset($data['sign'])?$data['sign']:'';
        $request_time = isset($data['request_time'])?$data['request_time']:0;
        if($request_time<(time()-300)){
            //$this->error('无效请求','',23);
        }
        if(empty($account)){
            $this->error('account参数错误','',22);
        }
        if(empty($requestSign)){
            $this->error('签名错误','',24);
        }
        $third_account = Db::name('kuyou_third_account')->where(['account'=>$account])->find();
        if(empty($third_account)){
            $this->error('账户不存在','',21);
        }elseif($third_account['status']!=1){
            $this->error('账户状态异常','',22);
        }
        $salt = $third_account['salt'];

        //校验签名数组
        $rule = [
            'test_goodstypelist' => 'account',//商品分类
            'test_getgoodslist' => 'account',//商品列表
            'test_getgoodsinfo' => 'account,id',//商品详情

            'test_joincustomers' => 'account,name,mobile',//添加客户
            'test_customersinfo' => 'account,mobile',//客户详情
            'test_editcustomers' => 'account,code,name,mobile',//更新客户

            'test_sales' => 'account,order_num,mobile,goods',//添加销货单
            'test_salesinfo' => 'account,purchase_code',//销货单详情
            'test_returnsales' => 'account,order_num,mobile,goods',//添加销货单
            'test_returnsalesinfo' => 'account,purchase_code',//销货单详情
        ];
        $controller_arr = explode('.',$controllername);
        $rule_name = $controller_arr[sizeof($controller_arr)-1].'_'.$actionname;
        if(!isset($rule[$rule_name])){
            $this->error('接口地址错误','',20);
        }

        //签名数组
        $sign_arr = [];
        $sign_key = explode(',',$rule[$rule_name]);
        foreach ($sign_key as $sign_key_k=>$sign_key_v){
            if(!isset($data[$sign_key_v])){
                $this->error($sign_key_v.'参数错误','',22);
            }
            $sign_arr[strtolower($sign_key_v)] = trim($data[$sign_key_v]);
        }
        ksort($sign_arr);
        foreach ($sign_arr as $sign_arr_k=>$sign_arr_v){
            $sign_arr[$sign_arr_k] = $sign_arr_k.'='.$sign_arr_v;
        }
        $sign_str = implode('&',$sign_arr).'key='.$salt;
        $sign = md5($sign_str);
        var_dump($sign);die;
    }
}
